import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:goumao_app/Controller/gm_about_page.dart';
import 'package:goumao_app/Controller/gm_account_deleted.dart';
import 'package:goumao_app/Controller/gm_cw_danan.dart';
import 'package:goumao_app/Controller/gm_empty_page.dart';
import 'package:goumao_app/Controller/gm_feed_back.dart';
import 'package:goumao_app/Controller/gm_mine_profile.dart';
import 'package:goumao_app/Controller/gm_my_pet_page.dart';
import 'package:goumao_app/Customs/gm_app_cache.dart';
import 'package:goumao_app/Customs/gm_app_util.dart';
import 'package:goumao_app/Customs/my_button.dart';
import 'package:goumao_app/Manager/gm_user_manager.dart';

import '../Customs/gm_native_channel.dart';
import 'gm_buy_vip_page.dart';
import 'gm_chat_list_page.dart';
import 'gm_my_qianbao.dart';

class ProfileScreen extends StatefulWidget {
  const ProfileScreen({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _ProfileScreenState createState() => _ProfileScreenState();
}

class _ProfileScreenState extends State<ProfileScreen> {
  final itemTextStyle = const TextStyle(
      color: Color(0xff333333), fontSize: 15, fontWeight: FontWeight.w500);
  List<String> xiaotieshi = [
    "定期兽医检查： 给你的宠物定期进行兽医检查，确保它们身体健康，及时发现并处理任何潜在问题",
    "合理饮食： 根据你宠物的种类和年龄提供适当的饮食。遵循兽医的建议，确保它们得到均衡的营养",
    "足够运动： 提供足够的运动和活动空间，不同种类的宠物需要不同程度的运动",
    "保持清洁： 定期梳理宠物的毛发，清理牙齿，并保持宠物生活区域的清洁",
    "交互动： 宠物需要社交互动，不同的动物有不同的社交需求。确保它们得到足够的关爱和陪伴",
    "培训和激励： 进行基本的宠物培训，帮助它们适应家庭规则。使用正面激励来奖励良好行为",
    "定期驱虫： 定期给宠物进行驱虫，以防止寄生虫感染",
    "提供安全环境： 确保宠物生活环境安全，远离对它们有害的物质和植物",
    "注意行为变化： 注意宠物的行为变化，这可能是健康问题的迹象。及时咨询兽医以获取建议",
    "标识宠物： 为宠物植入微芯片或佩戴标签，以便在它们走失时能够更容易地找到它们"
  ];
  int index = Random().nextInt(10);

  // @override
  // void initState() {
  //   super.initState();
  //   setState(() {});
  // }

  // ignore: non_constant_identifier_names
  Widget _TopButton(BuildContext context, int tag) {
    final path = tag == 0
        ? 'assets/images/gm_wd_hyzx_btn@2x.png'
        : 'assets/images/gm_wd_qb_btn@2x.png';
    return Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(4),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.5), // 设置阴影颜色
              spreadRadius: 1, // 设置阴影扩散的程度
              blurRadius: 3, // 设置阴影模糊的程度
              offset: const Offset(0, 1.5), // 设置阴影偏移量
            ),
          ],
        ),
        padding: const EdgeInsets.only(left: 12, right: 12),
        height: 70,
        width: (ScreenUtil().screenWidth - 35) * 0.5,
        child: InkWell(
          onTap: () => {
            if (tag == 0)
              {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const GMVIPCenterScreen(),
                  ),
                )
              }
            else
              {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const GMMyQianbaoScreen(),
                  ),
                )
              }
          },
          child: Image.asset(path),
        ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
          decoration: const BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/gm_wd_bg@2x.png'), // 替换为你的图片路径
              fit: BoxFit.cover,
              alignment: Alignment.center,
              repeat: ImageRepeat.noRepeat, // 可选，平铺模式
            ),
          ),
          child: SafeArea(
            child: Column(
              children: [
                Container(
                  height: 44.0,
                  padding: const EdgeInsets.only(left: 12, right: 12),
                  child: Stack(
                    children: [
                      const SizedBox(
                        width: double.infinity,
                        height: double.infinity,
                        // 可以添加其他Container的样式属性，例如padding、margin等
                      ),
                      const Align(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            "我的",
                            style: TextStyle(
                                color: Color(0xff1a1a1a),
                                fontSize: 17,
                                fontWeight: FontWeight.w600),
                          )),
                      Align(
                          alignment: Alignment.centerRight,
                          child: InkWell(
                            onTap: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                  builder: (context) => const GMChatListPage(),
                                ),
                              );
                            },
                            child: Image.asset(
                              'assets/images/gm_xx_zx_icon.png',
                              width: 24,
                            ),
                          )),
                    ],
                  ),
                ),
                // 可滑动的页面
                Expanded(
                  child: SingleChildScrollView(
                    child: Column(
                      children: [
                        Container(
                          padding: const EdgeInsets.fromLTRB(15, 25, 15, 16),
                          child: Column(
                            children: [
                              Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  ClipRRect(
                                    borderRadius: BorderRadius.circular(6),
                                    child: Image.network(
                                      GMUserManager().user!.icon,
                                      width: 50,
                                      height: 50,
                                    ),
                                  ),
                                  const SizedBox(
                                    width: 12,
                                  ),
                                  SizedBox(
                                    height: 44,
                                    child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        Row(
                                          children: [
                                            Text(
                                              GMUserManager().user!.nick,
                                              style: const TextStyle(
                                                  color: Colors.red,
                                                  fontSize: 18,
                                                  fontWeight: FontWeight.w800),
                                            ),
                                            const SizedBox(width: 8,),
                                            Image.asset("assets/images/gm_wz_vip_icon@2x.png", width: 40, height: 15,)
                                          ],
                                        ),
                                        Text("ID: ${GMUserManager().user!.ids}",
                                            style: const TextStyle(
                                              color: Color(0xff666666),
                                              fontSize: 12,
                                            ))
                                      ],
                                    ),
                                  ),
                                  const Spacer(),
                                  Container(
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(14),
                                      color: const Color(0xff73ed94),
                                    ),
                                    width: 80,
                                    height: 28,
                                    child: InkWell(
                                      onTap: () => {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder: (context) =>
                                                const GMUserProfilePage(),
                                          ),
                                        )
                                      },
                                      child: const Center(
                                        child: Text(
                                          "个人主页",
                                          style: TextStyle(
                                              color: Color(0xffffffff),
                                              fontSize: 12,
                                              fontWeight: FontWeight.w200),
                                        ),
                                      ),
                                    ),
                                  )
                                ],
                              ),
                              const SizedBox(
                                height: 20,
                              ),
                              Row(
                                children: [
                                  GMMyButton(
                                    direction: GMMyButtonDirection.top,
                                    text: "足迹",
                                    image: Image.asset(
                                      "assets/images/gm_zj_icon@2x.png",
                                      width: 20,
                                      height: 20,
                                    ),
                                    onTap: () {
                                      Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                          builder: (context) =>
                                              const GMEmptyPage(
                                            title: "我的足迹",
                                            emptyDes: "暂无数据～",
                                          ),
                                        ),
                                      );
                                    },
                                  ),
                                  const SizedBox(
                                    width: 44,
                                  ),
                                  GMMyButton(
                                    direction: GMMyButtonDirection.top,
                                    text: "收藏",
                                    image: Image.asset(
                                      "assets/images/gm_wdsc_icon@2x.png",
                                      width: 20,
                                      height: 20,
                                    ),
                                    onTap: () {
                                      Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                          builder: (context) =>
                                              const GMEmptyPage(
                                            title: "我的收藏",
                                            emptyDes: "暂无收藏～",
                                          ),
                                        ),
                                      );
                                    },
                                  ),
                                ],
                              ),
                              const SizedBox(
                                height: 16,
                              ),
                              SizedBox(
                                height: 70,
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    _TopButton(context, 0),
                                    _TopButton(context, 1),
                                  ],
                                ),
                              ),
                              const SizedBox(
                                height: 16,
                              ),
                              Container(
                                padding:
                                    const EdgeInsets.fromLTRB(12, 16, 12, 16),
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(4),
                                  boxShadow: [
                                    BoxShadow(
                                      color: Colors.grey
                                          .withOpacity(0.5), // 设置阴影颜色
                                      spreadRadius: 1, // 设置阴影扩散的程度
                                      blurRadius: 3, // 设置阴影模糊的程度
                                      offset: const Offset(0, 1.5), // 设置阴影偏移量
                                    ),
                                  ],
                                ),
                                child: Column(
                                  children: [
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        const Text(
                                          "我的宠物",
                                          style: TextStyle(
                                              color: Color(0xff1a1a1a),
                                              fontSize: 15,
                                              fontWeight: FontWeight.w200),
                                        ),
                                        InkWell(
                                          onTap: () => {
                                            Navigator.push(
                                              context,
                                              MaterialPageRoute(
                                                builder: (context) =>
                                                    const GMMyPetScreen(),
                                              ),
                                            )
                                          },
                                          child: const Text(
                                            "查看全部宠物",
                                            style: TextStyle(
                                                color: Color(0xff666666),
                                                fontSize: 12,
                                                fontWeight: FontWeight.w100),
                                          ),
                                        )
                                      ],
                                    ),
                                    const SizedBox(
                                      height: 12,
                                    ),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        ClipRRect(
                                          borderRadius:
                                              BorderRadius.circular(19),
                                          child: Image.network(
                                            "https://assets.buyonepet.com/75d4c000da2ca2e424f09cbcb0cc3813_700_787.jpg",
                                            width: 38,
                                            height: 38,
                                            fit: BoxFit.cover,
                                          ),
                                        ),
                                        const SizedBox(
                                          width: 10,
                                        ),
                                        Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            GMMyButton(
                                              text: "金陵王",
                                              image: Image.asset(
                                                "assets/images/gm_xb_nan@2x.png",
                                                width: 15,
                                                height: 15,
                                              ),
                                              direction:
                                                  GMMyButtonDirection.left,
                                              textStyle: const TextStyle(
                                                  color: Color(0xff333333),
                                                  fontSize: 14,
                                                  fontWeight: FontWeight.w500),
                                            ),
                                            const Text(
                                              "金毛|相伴2月1天",
                                              style: TextStyle(
                                                  color: Color(0xff666666),
                                                  fontSize: 10),
                                            )
                                          ],
                                        ),
                                        const Spacer(),
                                        InkWell(
                                          onTap: () => {
                                            Navigator.push(
                                              context,
                                              MaterialPageRoute(
                                                builder: (context) =>
                                                    const GMCWDananScreen(),
                                              ),
                                            )
                                          },
                                          child: Image.asset(
                                            "assets/images/gm_wd_tj_btn.png",
                                            width: 24,
                                            height: 24,
                                          ),
                                        ),
                                      ],
                                    ),
                                    const SizedBox(
                                      height: 12,
                                    ),
                                    Container(
                                      color: const Color(0xffeeeeee),
                                      height: 0.5,
                                    ),
                                    const SizedBox(
                                      height: 12,
                                    ),
                                    const Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Text(
                                          "每日养宠小贴士",
                                          style: TextStyle(
                                              color: Color(0xff73ed94),
                                              fontSize: 11,
                                              fontWeight: FontWeight.w600),
                                        ),
                                        // GMMyButton(
                                        //   text: "有用",
                                        //   image: Image.asset(
                                        //     "assets/images/gm_wd_yy_icon.png",
                                        //     width: 13,
                                        //     height: 13,
                                        //   ),
                                        //   direction: GMMyButtonDirection.left,
                                        //   textStyle: const TextStyle(
                                        //       color: Color(0xff555555),
                                        //       fontSize: 11,
                                        //       fontWeight: FontWeight.w600),
                                        // )
                                      ],
                                    ),
                                    const SizedBox(
                                      height: 12,
                                    ),
                                    Text(
                                      xiaotieshi[index],
                                      style: const TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: 14,
                                          fontWeight: FontWeight.w500),
                                    ),
                                  ],
                                ),
                              ),
                              const SizedBox(
                                height: 16,
                              ),
                              Container(
                                height: 170,
                                padding:
                                    const EdgeInsets.fromLTRB(0, 16, 0, 16),
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(4),
                                  boxShadow: [
                                    BoxShadow(
                                      color: Colors.grey
                                          .withOpacity(0.5), // 设置阴影颜色
                                      spreadRadius: 1, // 设置阴影扩散的程度
                                      blurRadius: 3, // 设置阴影模糊的程度
                                      offset: const Offset(0, 1.5), // 设置阴影偏移量
                                    ),
                                  ],
                                ),
                                child: GridButtons(),
                              ),
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ],
            ),
          )),
    );
  }
}

// ignore: must_be_immutable
class GridButtons extends StatelessWidget {
  List<String> values = [
    '领养记录=assets/images/gm_lyjl_icon@2x.png',
    '安全中心=assets/images/gm_aq_zx_icon@2x.png',
    '关于我们=assets/images/gm_gy_wm_icon@2x.png',
    '分享好友=assets/images/gm_fxhy_icon@2x.png',
    '用户反馈=assets/images/gm_yhfk_icon@2x.png',
    '应用评分=assets/images/gm_sz_icon@2x.png',
  ];

  GridButtons({super.key});

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 4, // 一行四个按钮
        crossAxisSpacing: 8.0, // 两个按钮之间的横向间距
        mainAxisSpacing: 8.0,
        childAspectRatio: 4 / 3,
      ),
      itemCount: values.length, // 总共七个按钮
      itemBuilder: (BuildContext context, int index) {
        List<String> valueArr = values[index].split('=');
        // 创建按钮
        return GMMyButton(
          text: valueArr.first,
          image: Image.asset(
            valueArr.last,
            width: 25,
            height: 25,
          ),
          direction: GMMyButtonDirection.top,
          onTap: () => {
            if (valueArr.first == '关于我们')
              {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const GMAboutMeScreen(),
                  ),
                )
              }
            else if (valueArr.first == '分享好友')
              {NativeChannel.shareApp("https://apps.apple.com/hk/app/tata%E4%BC%B4/id6474584711", 'TaTa伴')}
            else if (valueArr.first == '领养记录')
              {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const GMEmptyPage(
                      title: "我的领养",
                      emptyDes: "快去领养一只宠物吧～",
                    ),
                  ),
                )
              }
            else if (valueArr.first == '安全中心')
              {
                GMAppUtil.popFromBottomView(context, "账户中心", [
                  ListItem(
                      text: '注销账户',
                      onTap: () => {
                            Navigator.of(context).pop(),
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => const GMLogoutUserPage(),
                              ),
                            ),
                          }),
                  ListItem(
                      text: '退出登录',
                      onTap: () => {
                            Navigator.of(context).pop(),
                            GMUserManager().logout()
                          })
                ])
              }
            else if (valueArr.first == '用户反馈')
              {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const GMFeedBackScreen(),
                  ),
                )
              }
            else if (valueArr.first == '应用评分')
              {NativeChannel.requestReview()}
            else if (valueArr.first == '清理缓存')
              {print('43334343434'), GMCache.clearCache}
          },
        );
      },
    );
  }
}
